﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SgAutoArchiveSpDal.Linq
{
    public class T_CARDINFO_Dal
    {

        /// <summary>
        /// 添加一条登记
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static int Add_TCARDINFO(T_CARDINFO en)
        {
            SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
            conn.T_CARDINFO.Add(en);
                conn.SaveChanges();
                return en.C_ID;
        }

    
        public static int Modify_Cardinfo(int id, T_CARDINFO en)
        {
            SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
            if (conn.T_CARDINFO.Select(c => c).Where(c => c.C_CARDNO == en.C_CARDNO && c.C_ID!= en.C_ID).Count() > 0)
                return 0;
            else
            {
                var query = conn.T_CARDINFO.Select(c => c).Where(c => c.C_ID == id);
                foreach (var n in query)
                {
                    #region 绑定数据
                    //n.C_CARDNO = en.C_CARDNO;
                    //n.C_D01 = en.C_D01;
                    //n.C_D02 = en.C_D02;
                    //n.C_DATE = en.C_DATE;
                    //n.C_DEPARTMENT = en.C_DEPARTMENT;
                    //n.C_DJDATE = en.C_DJDATE;
                    //n.C_FANAME = en.C_FANAME;
                    //n.C_FANO = en.C_FANO;
                    //n.C_HJADDR = en.C_HJADDR;
                    n.C_I01 = en.C_I01;
                    //n.C_I02 = en.C_I02;
                    //n.C_ID = en.C_ID;
                    //n.C_MONAME = en.C_MONAME;
                    //n.C_MONO = en.C_MONO;
                    //n.C_NAME = en.C_NAME;
                    //n.C_PODATE = en.C_PODATE;
                    //n.C_PONAME = en.C_PONAME;
                    //n.C_PONO = en.C_PONO;
                    //n.C_POSEX = en.C_POSEX;
                    //n.C_SEX = en.C_SEX;
                    //n.C_SONDATE = en.C_SONDATE;
                    //n.C_SONNAME = en.C_SONNAME;
                    //n.C_SONNO = en.C_SONNO;
                    //n.C_SONSEX = en.C_SONSEX;
                    n.C_STAUTS = en.C_STAUTS;
                    //n.C_TYPEID = en.C_TYPEID;
                    //n.C_TYPENAME = en.C_TYPENAME;
                    //n.C_UNAME = en.C_UNAME;
                    n.C_V01 = en.C_V01;
                    //n.C_V02 = en.C_V02;
                    //n.C_V03 = en.C_V03;
                    //n.C_V05 = en.C_V05;
                    //n.C_YXDATE = en.C_YXDATE;
                    #endregion
                }
                conn.SaveChanges();
                return 0;
            }
        }
        public static List<T_CARDINFO> Query_SP()
        {
            try
            {
                SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                
                    query = query.Where(c => c.C_I01==0 && c.C_STAUTS==2);
               
                return query.ToList();
            }
            catch { return null; }
        }
        public static List<T_CARDINFO> Query_SPDC()
        {
            try
            {
                SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
                var query = conn.T_CARDINFO.Select(c => c);

                query = query.Where(c => c.C_I01 == -1||c.C_I01==1 &&c.C_STAUTS==2);

                return query.ToList();
            }
            catch { return null; }
        }
        public static List<T_CARDINFO> Query_Cardno(string cardno , int stauts)
        {
            try
            {
                SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (cardno!="" && cardno!=null)
                    query = query.Where(c => c.C_CARDNO == cardno);
                if (stauts != 0)
                    query = query.Where(c => c.C_STAUTS == stauts);
                return query.ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// 登陆用户查询未结束
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<T_CARDINFO> Query_TCarduname(string username)
        {
            try
            {
                SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (username != "")
                    query = query.Where(c => c.C_UNAME==username&&c.C_STAUTS!=3);
                return query.ToList();
            }
            catch { return null; }
        }

        /// <summary>
        /// 登陆用户查询未提交
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static List<T_CARDINFO> Query_TCarduname1(string username)
        {
            try
            {
                SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (username != "")
                    query = query.Where(c => c.C_UNAME == username && c.C_STAUTS == 1);
                return query.ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<T_CARDINFO> Query_Cardid(int id)
        {
            try
            {
                SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (id != 0)
                    query = query.Where(c => c.C_ID == id);
                return query.ToList();
            }
            catch { return null; }
        }
       
        /// <summary>
        /// 查询历史记录
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>

        public static List<T_CARDINFO> Query_log(string username)
        {
            try
            {
                SgAutoArchiveSpEntities conn = new SgAutoArchiveSpEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (username != "")
                    query = query.Where(c => c.C_UNAME == username && c.C_I01==2);
                return query.ToList();
            }
            catch { return null; }
        }
    }
}
